<?php

namespace Admin\Controller;
use User\Api\UserApi;

/**
 * 后台用户控制器
 */
class SystemController extends AdminController
{

    public function _initialize()
    {

        //项目列表
        $where['status'] = array('eq', 1);
        $project = M('project')->where($where)->order('id desc')->select();
        $this->assign('project', $project);
        parent::_initialize();
    }


//文章评论列表
    public function index()
    {

        $get = $_GET;
        $this->assign('get', $get);
        $time = timeCondition($get['start_time'], $get['end_time']);
        if ($time != 0) {
            $where['create_time'] = $time;
        }
        if ($get['title'] != '') {
            $where['content|title'] = array('like', '%' . $get['title'] . '%');
        }
        $where['status'] = array('gt', -1);

        //分页
        $listsCount = M('system_message')->where($where)->count();
        $Page = new \Think\Page($listsCount, 15);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出

        $list = M('system_message')->where($where)->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();

        $this->assign('list', $list);
        $this->assign('_page', $show);

        $this->display();
    }

//新增 (极光推送，短信消息)
    public function add()
    {
        if (IS_POST) {
            $post = $_POST;
            if (D('System')->create()) {
                if(!check_verify($post['verify'])){
                    $this->error('验证码错误');exit;
                }
                $post['create_time'] = time();
                $res = M('system_message')->add($post);
                if($res) {
                    //给业主发送短信
                    $mobile = find_house_man($post['project_id']);
                    $rest = mobile_msg_api($mobile,$post['content']);
					
					//极光推送
					$cjman = find_project_man($post['project_id']);
					$bsarr = implode(',',array_column($cjman,'tel_unique'));
					$ertt =  jpush_send($bsarr,$post['content'],array('type'=>2),'8e11c85ae983fa97d0766553','d29625fb339b1d960bf48627');
                    if($rest>0){
                        $this->success('发布成功', U('index'));
                    }else{
                        $this->success('内容发布成功，短信发布失败', U('index'));
                    }
                } else {
                    $this->error('发布失败');
                }
            } else {

                $this->error(D('System')->getError());
            }
        } else {
			
            $this->display();
        }
    }






}
